import { ValidatorFn, AbstractControl, ValidationErrors } from '@angular/forms';

/**
 * 方法传入验证
 * 
 * @param f 验证方法，返回true代表验证通过
 */
export function func(f: (value: string) => boolean, name: string = 'func'): ValidatorFn {
  return (control: AbstractControl): ValidationErrors | null => {
    if (control.value == null || control.value.length == 0) {
      return null;
    }
    if (f instanceof Function) {
      if (!f(control.value)) {
        let result: any = { error: true };
        result[name] = true;
        return result;
      }
    } else {
      console.error('Func Validator input params is not Function.');
    }

    return null;
  };
}